﻿<%@ page contentType="text/plain; charset=UTF-8"%>
<%@ page language="java"%>
<%@page import="com.tjsinfo.tjsoa.system.vo.User"%>
<%@ page import="java.util.*,com.seeyon.v3x.rota.manager.*,com.seeyon.v3x.rota.domain.*,.DayFormat,org.json.simple.JSONObject,org.json.simple.JSONArray,com.tjsinfo.tjsoa.duty.*,java.util.*"%>
<%@page import="com.tjsinfo.tjsoa.duty.DataCenterDao,com.tjsinfo.tjsoa.duty.IDataCenterDao"%>

<%!
    //获取目标月份任务信息
    String getTasks(String month) {
        JSONArray array = new JSONArray();      //新建JSON数组对象
        RotaManagerImpl trdm=new RotaManagerImpl();
        List<TResourceDuty> list=trdm.findByLikeProperty("dutyDate",month + "-%");
        for(TResourceDuty trd:list){
        	JSONObject obj = new JSONObject();
                obj.put("id", trd.getId());
                obj.put("task", trd.getName());
                obj.put("builddate", trd.getDutyDate());
                obj.put("taskType", trd.getType());
                array.add(obj);
        }
        return array.toString();
    }

    //添加新任务
    int addTask(String taskInfo, String buildDate, String taskType,User user) {
        int newId = -1;                         //用于保存新任务编号
        TResourceDutyManager trdm=new TResourceDutyManagerImpl();
        TResourceDuty trd=new TResourceDuty();
        trd.setDutyDate(buildDate);
        trd.setType(taskType);
        trd.setName(taskInfo);
        int tempId=trdm.save(trd);
        try{
        	//**TSystemLog tsp=new TSystemLog();
        	//Date dateT = new Date();
			//dateT=DayFormat.getCurrentUtilDate();
			//tsp.setCreateDate(dateT);
			//tsp.setName(user.getUserName());
			//tsp.setTodo("值班");
			//tsp.setTitle(taskInfo);
			//tsp.setUserId(user.getUserId().toString());
			//tsp.setSortId("29");
			//SystemFactory.saveLog(tsp);
        }catch(Exception e) {
			e.printStackTrace();
		}
        return tempId;
    }

    //删除任务 
    void delTask(String taskId) {
        TResourceDutyManager trdm=new TResourceDutyManagerImpl();
        TResourceDuty trd=trdm.findById(new Integer(taskId));
        trdm.delete(trd);
    }

    //更新任务
    void updateTask(String taskId, String taskInfo, String taskType) {
        TResourceDutyManager trdm=new TResourceDutyManagerImpl();
        TResourceDuty trd=new TResourceDuty();
        trd.setId(new Integer(taskId));
        trd.setType(taskType);
        trd.setName(taskInfo);
        IDataCenterDao idcd=new DataCenterDao();
        idcd.update(trd);
    }
%>
<%
    out.clear();                                                //清空当前的输出内容（空格和换行符）
    request.setCharacterEncoding("UTF-8");                      //设置请求字符集为UTF-8
	User user = (User)session.getAttribute("user");

    String action = request.getParameter("action");             //获取action信息

    //根据action不同执行不同的操作
    if ("addTask".equals(action)) {                             //新建任务
        String taskInfo = request.getParameter("taskInfo");
        String buildDate = request.getParameter("buildDate");
        String taskType = request.getParameter("taskType");
        out.print(addTask(taskInfo,buildDate,taskType,user));
    } else if ("getTasks".equals(action)) {                     //获取整月任务信息
        String month = request.getParameter("month");
        String result = getTasks(month);
        out.println(result);
    } else if ("delTask".equals(action)) {                      //删除任务
        String taskId = request.getParameter("taskId");
        delTask(taskId);
    } else if ("updateTask".equals(action)) {                   //更新任务信息
        String taskId = request.getParameter("taskId");
        String taskInfo = request.getParameter("taskInfo");
        String taskType = request.getParameter("taskType");
        updateTask(taskId, taskInfo, taskType);
    }
%>